package com.tz.liveplayermodule.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.tz.liveplayermodule.R;
import com.tz.liveplayermodule.bean.PlayBackListBean;
import com.tz.liveplayermodule.util.CommonUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Rusan on 2017/5/15.
 */

public class RecyclerAdapter extends SecondaryListAdapter<RecyclerAdapter.GroupItemViewHolder, RecyclerAdapter.SubItemViewHolder> {
    private Context context;
    private List<DataTree> dts = new ArrayList<>();
    private OnGroupClickListener onGroupClickListener;
    private OnItemClickListener onItemClickListener;

    public RecyclerAdapter(Context context) {
        this.context = context;
    }

    public void setData(List datas) {
        dts = datas;
        notifyNewData(dts);
    }

    @Override
    public RecyclerView.ViewHolder groupItemViewHolder(ViewGroup parent) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_play_title, parent, false);
        return new GroupItemViewHolder(v);
    }

    @Override
    public RecyclerView.ViewHolder subItemViewHolder(ViewGroup parent) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_play_item, parent, false);
        return new SubItemViewHolder(v);
    }

    @Override
    public void onGroupItemBindViewHolder(RecyclerView.ViewHolder holder, int groupItemIndex) {
        ((GroupItemViewHolder) holder).itemPlayTitleText.setText((String) dts.get(groupItemIndex).getGroupItem());
    }

    @Override
    public void onSubItemBindViewHolder(RecyclerView.ViewHolder holder, int groupItemIndex, int subItemIndex) {
        PlayBackListBean playBackListBean = (PlayBackListBean) dts.get(groupItemIndex).getSubItems().get(subItemIndex);
        ((SubItemViewHolder) holder).playItemTitle.setText(playBackListBean.getTitle());

        //0 是没有 1等待直播 2即将直播 3正在直播 4直播结束
        if (playBackListBean.getType() == 0) {
            String playType = "";
            switch (playBackListBean.getIs_live()) {
                case 0:
                    break;
                case 1:
                    playType = "  (等待直播)";
                    break;
                case 2:
                    playType = "  (即将直播)";
                    break;
                case 3:
                    playType = "  (正在直播)";
                    break;
                case 4:
                    playType = "  (直播结束)";
                    break;
                default:
                    break;
            }

            ((SubItemViewHolder) holder).playItemTimeText.setText("时长：" + playBackListBean.getStart_time()
                    + "-" + playBackListBean.getEnd_time() + playType);
        } else if (playBackListBean.getType() == 1) {
            ((SubItemViewHolder) holder).playItemTimeText.setText("时长：" + playBackListBean.getDuration());
        } else if (playBackListBean.getType() == 2) {
            ((SubItemViewHolder) holder).playItemTimeText.setText("时长：" + playBackListBean.getDuration());
        } else if (playBackListBean.getType() == 3) {
            ((SubItemViewHolder) holder).playItemTimeText.setText("时长：" + playBackListBean.getTime());
        } else if (playBackListBean.getType() == 4) {
//            ((SubItemViewHolder) holder).playItemTimeText.setText("时长：" + CommonUtils.stringForTime(Integer.parseInt(playBackListBean.getDuration())));
            ((SubItemViewHolder) holder).playItemTimeText.setText("时长：" + playBackListBean.getDuration());
        }
    }

    @Override
    public void onGroupItemClick(Boolean isExpand, GroupItemViewHolder holder, int groupItemIndex) {
        System.out.println("groupItemIndex:" + groupItemIndex);
        onGroupClickListener.onGroupClickListener(groupItemIndex);
        if (isExpand) {
            holder.itemPlayTitleImg.setImageResource(R.mipmap.group_down);
        } else {
            holder.itemPlayTitleImg.setImageResource(R.mipmap.group_up);
        }
        notifyDataSetChanged();
//        Toast.makeText(context, "group item " + String.valueOf(groupItemIndex) + " is expand " +
//                String.valueOf(isExpand), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSubItemClick(SubItemViewHolder holder, int groupItemIndex, int subItemIndex) {
        onItemClickListener.onItemClickListener(groupItemIndex, subItemIndex);
//        Toast.makeText(context, "sub item " + String.valueOf(subItemIndex) + " in group item " +
//                String.valueOf(groupItemIndex), Toast.LENGTH_SHORT).show();
    }

    public static class GroupItemViewHolder extends RecyclerView.ViewHolder {
        TextView itemPlayTitleText;
        ImageView itemPlayTitleImg;

        public GroupItemViewHolder(View itemView) {
            super(itemView);
            itemPlayTitleText = (TextView) itemView.findViewById(R.id.item_play_title_text);
            itemPlayTitleImg = (ImageView) itemView.findViewById(R.id.item_play_title_img);
        }
    }

    public static class SubItemViewHolder extends RecyclerView.ViewHolder {
        TextView playItemTitle;
        TextView playItemTimeText;

        public SubItemViewHolder(View itemView) {
            super(itemView);
            playItemTitle = (TextView) itemView.findViewById(R.id.play_item_title);
            playItemTimeText = (TextView) itemView.findViewById(R.id.play_item_time_text);
        }
    }

    public interface OnGroupClickListener {
        void onGroupClickListener(int groupItemIndex);
    }

    public void setOnGroupClickListener(OnGroupClickListener onGroupClickListener) {
        this.onGroupClickListener = onGroupClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(int groupItemIndex, int subItemIndex);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}

